package com.ruoyi.project.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.mapper.CollegeMapper;
import com.ruoyi.project.system.mapper.CourseMapper;
import com.ruoyi.project.system.mapper.CourseStudyMapper;
import com.ruoyi.project.system.mapper.StudentMapper;
import com.ruoyi.project.system.service.ICourseStudyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 课程学习Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-07
 */
@Service
public class CourseStudyServiceImpl implements ICourseStudyService
{
    @Autowired
    private CourseStudyMapper courseStudyMapper;

    @Autowired
    private CollegeMapper collegeMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private CourseMapper courseMapper;


    /**
     * 查询课程学习
     * 
     * @param id 课程学习ID
     * @return 课程学习
     */
    @Override
    public CourseStudy selectCourseStudyById(Integer id)
    {
        return courseStudyMapper.selectCourseStudyById(id);
    }

    /**
     * 查询课程学习列表
     * 
     * @param courseStudy 课程学习
     * @return 课程学习
     */
    @Override
    public List<CourseStudy> selectCourseStudyList(CourseStudy courseStudy)
    {
        return courseStudyMapper.selectCourseStudyList(courseStudy);
    }

    /**
     * 新增课程学习
     * 
     * @param courseStudy 课程学习
     * @return 结果
     */
    @Override
    public int insertCourseStudy(CourseStudy courseStudy)
    {
        SysUser sysUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest()).getUser();
        if(sysUser.getType()==1){

            Student student = studentMapper.selectStudentById(Integer.parseInt(sysUser.getEmail()));
            College college = collegeMapper.selectCollegeById(Integer.valueOf((int) sysUser.getDeptId().longValue()));
            Course course = courseMapper.selectCourseById(courseStudy.getCourseId().intValue());
            courseStudy.setCourseTitle(course.getTitle());
            courseStudy.setMemberId(student.getMemberId());
            courseStudy.setStudentId(student.getId().longValue());
            courseStudy.setStudentName(student.getRealName());
            courseStudy.setCollegeId(college.getCollegeId().longValue());
            courseStudy.setCollegeCode(college.getCollegeCode());
            courseStudy.setCollegeName(college.getCollegeName());
            courseStudy.setCreateDt(new Date());
            collegeMapper.updateCount(courseStudy.getCourseId());

            return courseStudyMapper.insertCourseStudy(courseStudy);

        }

        return 1;

    }

    /**
     * 修改课程学习
     * 
     * @param courseStudy 课程学习
     * @return 结果
     */
    @Override
    public int updateCourseStudy(CourseStudy courseStudy)
    {
        return courseStudyMapper.updateCourseStudy(courseStudy);
    }

    /**
     * 批量删除课程学习
     * 
     * @param ids 需要删除的课程学习ID
     * @return 结果
     */
    @Override
    public int deleteCourseStudyByIds(Integer[] ids)
    {
        return courseStudyMapper.deleteCourseStudyByIds(ids);
    }

    /**
     * 删除课程学习信息
     * 
     * @param id 课程学习ID
     * @return 结果
     */
    @Override
    public int deleteCourseStudyById(Integer id)
    {
        return courseStudyMapper.deleteCourseStudyById(id);
    }


    /**
     * 今日学习次数图表里面所需要的值
     * @return
     */
    @Override
    public Map<String, Object> getSumDayStudyNum() {
        Map<String,Object> map = new HashMap<>();
        //1,先获取课程学习总次数
        int sum = courseStudyMapper.selectSumCount();
        map.put("sumCount",sum);
        List<String> courseNameList = courseStudyMapper.selectCourseName();
        map.put("courseNameList",courseNameList);
        List<Integer> courseCountList = courseStudyMapper.selectCourseCount();
        map.put("courseCountList",courseCountList);
        return map;
    }


    /**
     * 今日学习次数图表里面所需要的值  加學下限制
     *
     * @param deptId
     * @return
     */

    @Override
    public Map<String, Object> getSumDayStudyNumCollegeId(Long deptId) {
        Map<String,Object> map = new HashMap<>();
        //1,先获取课程学习总次数
        int sum = courseStudyMapper.selectSumCountCollegeId(deptId);
        map.put("sumCount",sum);
        List<String> courseNameList = courseStudyMapper.selectCourseNameCollegeId(deptId);
        map.put("courseNameList",courseNameList);
        List<Integer> courseCountList = courseStudyMapper.selectCourseCountCollegeId(deptId);
        map.put("courseCountList",courseCountList);
        return map;
    }
}
